
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse, glue)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
  theme_set(lal_plot_theme())
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
  theme_set(theme_minimal())
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Load the synth did estimates
load(file = file.path(root, "tmp/wi_allsynth.RData"), verbose = TRUE)

# Set up the matrix for storing output
output = matrix(nrow=6, ncol=8)
estimators = c("vs_did", "vs_time_wts", "vs_county_wts", "vs_sdid",
  "to_did", "to_time_wts", "to_county_wts", "to_sdid")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(output) = estimators
rownames(output) = output_components

# Store the estimated effects and standard errors
output[1:2,] = cbind(wi_allsynth_dem$summary_table[,c(1, 4, 9, 3)],
  wi_allsynth_tur$summary_table[,c(1, 9, 4, 3)])

# Count the number of years and units
c = 1
for(all_ests in list(wi_allsynth_dem, wi_allsynth_tur)){
  for(e_col in c("DID", "Time Weighted DID", 
      "DIFP (Regularized)", "Synthetic DID (SDID)")){
    est_setup = attr(all_ests$ests[[e_col]], "setup")
    output[3,c] = est_setup$T0 + 1
    output[4,c] = nrow(est_setup$Y) - est_setup$N0
    output[5,c] = nrow(est_setup$Y)
    output[6,c] = nrow(est_setup$Y)*ncol(est_setup$Y)
    c = c + 1
  }
}

# Put the main estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in Wisconsin in 2020.}",
"\\label{tab:wi}}",
"\\begin{tabular}{lcccc|cccc}",
"\\toprule \\toprule",
" & \\multicolumn{4}{c}{Dem Vote Share (\\%)} & \\multicolumn{4}{c}{Turnout (\\%)} \\\\",
" & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(output[1,]), " \\\\"),
paste0(prep_table_rows(output[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(output[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(output[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(output[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"County Weights & No & No & Yes & Yes & No & No & Yes & Yes \\\\",
"Year Weights & No & Yes & No & Yes & No & Yes & No & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{9}{p{1\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/wi_effects_table.tex"), sep="\n")
